package com.zxj.camerademo.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.util.Log;

import com.zxj.camerademo.camera2.OnSavePicState;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

import okio.Okio;

public class BitmapUtils {

    private static final String TAG = BitmapUtils.class.getSimpleName();

    public static byte[] toByteArray(Bitmap bitmap) {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
        return os.toByteArray();
    }

    public static Bitmap decodeBitmap(Bitmap bitmap,int reqWidth,int reqHeight){
        BitmapFactory.Options options = new BitmapFactory.Options();

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG,100,bos);
        BitmapFactory.decodeByteArray(bos.toByteArray(),0,bos.size(),options);

        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeByteArray(bos.toByteArray(),0,bos.size(),options);
    }

    private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        int rawWidth = options.outWidth;
        int rawHeight = options.outHeight;
        int inSampleSize = 1;

        if (rawWidth > reqWidth || rawHeight > reqHeight) {
            int halfWidth = rawWidth / 2;
            int halfHeight = rawHeight / 2;
            while ((halfWidth / inSampleSize) > reqWidth && (halfHeight / inSampleSize) > reqHeight) {
                inSampleSize *= 2;  //设置inSampleSize为2的幂是因为解码器最终还是会对非2的幂的数进行向下处理，获取到最靠近2的幂的数
            }
        }
        return inSampleSize;
    }

    public static Bitmap mirror(Bitmap rawBitmap) {
        Matrix matrix = new Matrix();
        matrix.postScale(-1f, 1f);
        return Bitmap.createBitmap(rawBitmap, 0, 0, rawBitmap.getWidth(), rawBitmap.getHeight(), matrix, true);
    }

    public static Bitmap rotate(Bitmap rawBitmap,Float degree ) {
        Matrix matrix = new Matrix();
        matrix.postRotate(degree);
        return Bitmap.createBitmap(rawBitmap, 0, 0, rawBitmap.getWidth(), rawBitmap.getHeight(), matrix, true);
    }

    public static void savePic(final byte[] data, final boolean isMirror, final OnSavePicState onSavePicState){
        new Thread(new Runnable() {
            @Override
            public void run() {
                long temp = System.currentTimeMillis();
                File picFile = FileUtil.createCameraFile("camera2");
                if(picFile != null && data != null){
                    Bitmap rawBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                    Bitmap resultBitmap = isMirror ?mirror(rawBitmap):rawBitmap;

                    try {
                        Okio.buffer(Okio.sink(picFile)).write(toByteArray(resultBitmap)).close();

                        onSavePicState.onSuccess(picFile.getAbsolutePath(),(System.currentTimeMillis() - temp));

                        Log.d(TAG,"图片已保存! 耗时："+(System.currentTimeMillis() - temp)+"    路径： "+picFile.getAbsolutePath());
                    } catch (IOException e) {
                        onSavePicState.onFailed(e.getMessage());
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}
